<template>
  <div class="dashboard-container">
    <div class="navbarchild">
      <div @click="$router.go(-1)">
        <i class="el-icon-back"></i>
        <span>返回</span>
      </div>
      <el-card style="height: 120px; margin-bottom: 20px; margin-top: 10px">
        <!-- 给el-form绑定form -->
        <el-form label-width="80px" :model="form" :inline="true">
          <el-form-item>
            <!-- 通过:value和v-model收集item项商品的id -->
            <el-select
              v-model="form.category1Id"
              placeholder="请选择省"
              :disabled="isShowselect1"
            >
              <el-option
                v-for="a in reqCategory1"
                :key="a.id"
                :label="a.name"
                :value="a.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="form.category2Id"
              placeholder="请选择市"
              :disabled="isShowselect2"
            >
              <el-option
                v-for="item in reqCategory2"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="form.category3Id"
              placeholder="请选择区"
              :disabled="isShowselect3"
            >
              <el-option
                v-for="c in reqCategory3"
                :key="c.id"
                :label="c.name"
                :value="c.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchclick"
            >查询</el-button
          >
          <el-button @click="resetSearch">清 空</el-button>
          <el-button type="success" @click="addguige">添加规格</el-button>
        </el-form>
      </el-card>
      <el-table :data="tableData" border>
        <el-table-column prop="serveid" label="服务id" align="center">
        </el-table-column>
        <el-table-column prop="name" label="名称" align="center">
        </el-table-column>
        <el-table-column prop="title" label="标题" align="center">
        </el-table-column>
        <el-table-column prop="price" label="价格" align="center">
        </el-table-column>
        <el-table-column
          prop="divide1"
          label="一级分佣比例 （百分比）"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="divide2"
          label="二级分佣比例 （百分比）"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="divide3"
          label="三级分佣比例 （百分比）"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="hour" label="一共几个小时" align="hour">
        </el-table-column>
        <el-table-column prop="bill" label="发单端提成比例" align="bill">
        </el-table-column>
        <el-table-column prop="city" label="城市" align="city">
        </el-table-column>
        <el-table-column prop="district" label="区县" align="district">
        </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="warning"
              @click="changerenyuan(scope.row)"
              size="small"
              >修改</el-button
            >
            <el-button type="danger" @click="deldel(scope.row)" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :currentPage="this.form.pageNo"
        :size="this.form.pageSize"
        :total="total"
        @changelimit="changelimit"
        @changecurrentPage="changecurrentPage"
      ></Pagination>
    </div>

    <add ref="add" @refresh="refresh"></add>
  </div>
</template>

<script>
import {
  getcitylist,
  getservelist,
  getserveguigelist,
  delserveguigedel,
} from "@/api/serveguigelist";
import add from "./modules/add";

export default {
  name: "Dashboard",
  components: {
    add,
  },

  data() {
    return {
      // 绑定表单信息
      form: {
        serveid: "",
        cityid: "",
        category1Id: "",
        category2Id: "",
        category3Id: "",
        adminid:Number(localStorage.getItem('userId'))
      },
      total: 0,
      tableData: [],
      // 省
      reqCategory1: [],
      // 市
      reqCategory2: [],
      // 区
      reqCategory3: [],
      // 第一个下拉框
      isShowselect1: false,
      // 第二个下拉框
      isShowselect2: false,
      isShowselect3: false,
    };
  },
  created() {
    console.log(this.$route.query.serveid);
    this.$nextTick(() => {
      this.form.serveid = Number(this.$route.query.serveid);
      this.getcitylist1();
      this.getserveguigelist1();
    });
  },
  methods: {
    async getserveguigelist1() {
      let result = await getserveguigelist({
        serveid: this.form.serveid,
        cityid: this.form.cityid,
        adminid:Number(localStorage.getItem('userId'))
      });
      console.log(result);
      this.tableData = result.data;
    },
    async getcitylist1() {
      let result = await getcitylist({adminid:Number(localStorage.getItem('userId'))});
      if (result.code == 0) {
        this.reqCategory1 = result.data;
        console.log(this.reqCategory1);
      }
    },
    async change(e, id) {
      let data = {
        id: Number(id),
        status: e ? 1 : 2,
        adminid:Number(localStorage.getItem('userId'))
      };
      console.log(data);
      let result = await shangxiaservesxj(data);
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success",
        });
      }
      console.log(result);
      this.getservelist1();
    },
    searchclick() {
      this.getserveguigelist1();
    },
    resetSearch() {
      this.form = {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      };
      // this.getservelist1();
    },
    addguige() {
      this.$refs.add.add({ serveid: this.form.serveid });
    },
    // 子传父:自定义事件
    refresh() {
      this.getserveguigelist1();
    },
    async getservelist1() {
      let result = await getservelist(this.form);
      console.log(result);
      if (result.code == 0) {
        this.tableData = result.data.data;
        this.total = result.data.totalCount;
        if (result.data.data != null) {
          result.data.data.forEach((item) => {
            if (item.status == 1) {
              this.$set(item, "switch", true);
            } else {
              this.$set(item, "switch", false);
            }
          });
        }
      }
    },
    changelimit(val) {
      this.form.pageSize = val;
      this.getservelist1();
    },
    changecurrentPage(val) {
      this.form.pageNo = val;
      this.getservelist1();
    },
    changerenyuan(val) {
      this.$refs.add.edit({
        ...val,
        serveid: this.form.serveid,
        category1Id: this.form.category1Id,
        category2Id: this.form.category2Id,
        category3Id: this.form.category3Id,
      });
    },
    async deldel(val) {
      let result = await delserveguigedel({ id: val.id,adminid:Number(localStorage.getItem('userId')) });
      if (result.code == 0) {
        this.$message({
          showClose: true,
          message: "删除成功",
          type: "error",
        });
        this.getserveguigelist1();
      }
    },
  },

  filters: {
    statusfilter(val) {
      if (val == 1) {
        return "正常";
      } else {
        return "推荐";
      }
    },
    datefilter(val) {
      var date = new Date(parseInt(val) * 1000);
      var Year = date.getFullYear();
      var Moth =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var Day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var Hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var Minute =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      var Sechond =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      var GMT =
        Year +
        "-" +
        Moth +
        "-" +
        Day +
        "   " +
        Hour +
        ":" +
        Minute +
        ":" +
        Sechond;

      return GMT;
    },
  },
  watch: {
    "form.category1Id": {
      handler(newVal) {
        if (newVal) {
          let arr = this.reqCategory1.filter((item) => item.id == newVal);
          this.reqCategory2 = arr[0].List;
          this.form.category2Id = "";
          this.form.category3Id = "";
          this.form.cityid = newVal;
          this.getcitylist1();
        }
      },
    },
    "form.category2Id": {
      handler(newVal) {
        if (newVal) {
          console.log(newVal);
          let arr = this.reqCategory2.filter((item) => item.id == newVal);
          this.reqCategory3 = arr[0].List;
          // console.log(newVal);
          this.form.category3Id = "";
          this.form.cityid = newVal;
          this.getcitylist1();
        }
      },
    },
    "form.category3Id": {
      handler(newVal) {
        if (newVal) {
          this.form.cityid = newVal;
          this.getcitylist1();
        }
      },
    },
  },
};
</script>

<style lang="scss" scoped>
// .dashboard {
//   &-container {
//     margin: 30px;
//   }
//   &-text {
//     font-size: 30px;
//     line-height: 46px;
//   }
// }
.navbarchild {
  background: white;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  height: 50px;
  // width: 90%;
  line-height: 50px;
  padding: 0px 20px;
  cursor: pointer;
}
// .dashboard {
//   &-container {
//     margin: 30px;
//   }
//   &-text {
//     font-size: 30px;
//     line-height: 46px;
//   }
// }
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 150px;
  height: 150px;
}

::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

::v-deep .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}

::v-deep .avatar {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;

  max-width: 150px;
  max-height: 150px;
  margin: 0 auto;
  display: block;
}
</style>
